// pages/Shopping Cart/Shopping Cart.js
const {
    ShopList,
    ShopCheck,
    ShopDel,
    ShopChange
} = require('../../http/api')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Shop: [],
        Price: 0,
    },
    getlist() {
        ShopList({
            token: wx.getStorageSync('token')
        }).then(res => {
            // console.log(res);
            this.setData({
                Shop: res.data,
                Price: res.data.price
            })
            console.log(this.data.Shop);
        })
    },
    // 修改状态
    ChangeCheck(e) {
        // console.log(e);
        let item = e.currentTarget.dataset.item
        // console.log(item.selected);
        ShopCheck({
            key: item.key,
            selected: !item.selected,
            token: wx.getStorageSync('token')
        }).then((res) => {
            console.log(res);
            this.setData({
                Price: res.data.price,
                Shop: res.data
            })
            console.log(this.data.Shop);
        })
    },
    // 删除购物车
    Del(e) {
        // console.log(e.currentTarget.dataset.obj);
        let obj = e.currentTarget.dataset.obj
        ShopDel({
            key: obj.key,
            token: wx.getStorageSync('token')
        }).then((res) => {
            this.CunChu(res)
            // console.log(res);
            this.setData({
                Shop: res.data,
                Price: res.data.price || 0,
            })
        })
    },
    // 修改购物车数量(加)
    onChange(e) {
        // console.log(e.currentTarget.dataset.val);
        let Val = e.currentTarget.dataset.val
        Val.number++
        ShopChange({
            key: Val.key,
            number: Val.number,
            token: wx.getStorageSync('token')
        }).then((res) => {
            // console.log(res);
            this.setData({
                Shop: res.data,
                Price: res.data.price
            })
        })
    },
    // 修改购物车数量(减)
    onChange1(e) {
        let Val = e.currentTarget.dataset.val
        Val.number--
        ShopChange({
            key: Val.key,
            number: Val.number,
            token: wx.getStorageSync('token')
        }).then((res) => {
            // console.log(res);
            this.setData({
                Shop: res.data,
                Price: res.data.price
            })
        })
    },
    CunChu(data) {
        wx.setStorageSync('shop', data.length)
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getlist()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})